import requests
import log

def get_alive_ids():
    url = "http://127.0.0.1:54345/browser/pids/all"
    body = {}
    try:
        # 发送 POST 请求，将 ids 作为 JSON 数据传递
        response = requests.post(url, json=body)
        # 检查响应状态码
        response.raise_for_status()
        temp = response.json()
        log.Info(temp)
        # 获取字典所有的键作为列表
        temp = temp["data"]
        data = list(temp.keys())
        log.Info(data)
        return data
    except requests.RequestException as e:
        log.Error(f"请求出错: {e}")

def get_pyppeteer_wslink_by_ids(id):
    url = "http://127.0.0.1:54345/browser/open"
    body = {
  "id": id,
  "args": [],
  "queue": True
}
    try:
        # 发送 POST 请求，将 ids 作为 JSON 数据传递
        response = requests.post(url, json=body)
        # 检查响应状态码
        response.raise_for_status()  
        temp = response.json()
        log.Info(temp)
        temp = temp["data"]["ws"]
        return temp
    except requests.RequestException as e:
        log.Error(f"请求出错: {e}")

def get_info_by_id(id):
    url = "http://127.0.0.1:54345/browser/open"
    body = {
  "id": id,
  "args": [],
  "queue": True
}
    try:
        # 发送 POST 请求，将 ids 作为 JSON 数据传递
        response = requests.post(url, json=body)
        # 检查响应状态码
        response.raise_for_status()  
        temp = response.json()
        log.Info(temp)
        temp = temp["data"]["name"]
        return {temp:id}
    except requests.RequestException as e:
        log.Error(f"请求出错: {e}")


def get_name_by_id(id):
    url = "http://127.0.0.1:54345/browser/detail"
    body = {
        "id": id
    }
    try:
        # 发送 POST 请求，将 ids 作为 JSON 数据传递
        response = requests.post(url, json=body)
        # 检查响应状态码
        response.raise_for_status()  
        temp = response.json()
        log.Info(temp)
        temp = temp["data"]
        return temp["seq"]
    except requests.RequestException as e:
        log.Error(f"请求出错: {e}")
if __name__ == "__main__":
    # temp = get_alive_ids()
    # temp = get_pyppeteer_wslink_by_ids(temp[0])
    # temp = get_pyppeteer_wslink_by_ids()
    print(get_name_by_id(get_alive_ids()[0]))
